<?php
require_once("Database.php");
require_once("DBConfig.php");
require_once("RegistrationView.php");
require_once("validate.php");

class RegistrationModel{
	
	private $m_db = NULL;
	
	public function __construct(Database $db){
		$this->m_db = $db;
	}
	
	public function ValidatePassword($password){
		if(strlen($password) < 6){
			return false;
		}
		else {
			return true;
		}
	}
	
	public function RegisterNewUser($username, $password){
			$validate = new Validate();

			if($this->ValidatePassword($password) && $validate->ValidatePasswordRegex($password) &&
			 $this->FindUserName($username) == FALSE){
				$this->m_db->InsertUser($username, sha1($password));
			}
			else{
				return false;
			}
			 
			 return true;
		
		
	}
	public function FindUserName($username){
			if($this->m_db->FindUserName($username) == 0){
				return false;
			}
			else{
				return true;
			}
	}
	
	public function test(){
		if($this->ValidatePassword("test")){
			return false;
		}
		if(!$this->ValidatePassword("testinglongerpassword")){
			return false;
		}
		if($this->FindUserName('Frank')){
			return false;
		}
		/*
		 * Testade RegisterNewUser - Fungerar helt!
		if(!$this->RegisterNewUser('Twilight', 'Sparkle', 'Sparkle')){
			return FALSE;
		}*/
		
		return true;
	}
}
?>